home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZFixedAllocator.h
< prev
next >
Wrap
Text File
|
1997-09-02
|
3KB
|
115 lines
/*
* File: ZFixedAllocator.h
* Summary: A class that can speedily allocate fixed size blocks.
* Written by: Jesse Jones
*
* Copyright ゥ 1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <2> 7/13/97 JDJ Inlined HasBlock.
* <1> 1/29/96 JDJ Created
*/
#pragma once
#include <ZDebug.h>
#include <ZTypes.h>
//-----------------------------------
// Forward References
//
struct SBlock;
//-----------------------------------
// Types
//
typedef void (*BlockValidateHook)(const void* block, long size, void* refCon);
// ===================================================================================
// class TFixedAllocator
// ===================================================================================
class TFixedAllocator {
//-----------------------------------
// Initialization/Destruction
//
public:
~TFixedAllocator();
TFixedAllocator(ulong blockSize, ulong maxBlocks);
private:
TFixedAllocator(const TFixedAllocator& rhs);
TFixedAllocator& operator=(const TFixedAllocator& rhs);
//-----------------------------------
// Allocations
//
public:
void* Allocate();
void Deallocate(void* ptr);
//-----------------------------------
// Info
//
public:
ulong GetHeapSize() const {return mBlockSize*mMaxBlocks;}
ulong GetCurrentPercent() const {return 100*mBlockCount/mMaxBlocks;}
// Returns the number of blocks as a percentage of the max blocks.
ulong GetMaxPercent() const {return 100*mMaxBlockCount/mMaxBlocks;}
// Returns the largest number of blocks as a percentage of the max blocks.
ulong GetBlockSize() const {return mBlockSize;}
bool HasBlock(const void* ptr) const;
// Returns true if the block belongs to 'this'.
#if DEBUG
virtual void ValidateHeap(BlockValidateHook hook = nil, void* refCon = nil) const;
#endif
//-----------------------------------
// Internal API
//
protected:
SBlock* GetNextBlock(SBlock* block) const;
//-----------------------------------
// Member Data
//
protected:
ulong mBlockSize;
ulong mMaxBlocks;
SBlock* mBlocks;
ulong mBlockCount;
ulong mMaxBlockCount;
SBlock* mNextFreeBlock;
SBlock* mEndBlock;
};
// ===================================================================================
// Inlines
// ===================================================================================
inline bool TFixedAllocator::HasBlock(const void* ptr) const // this is often called *lots* of times
{
ASSERT(ptr != nil);
const SBlock* block = reinterpret_cast<const SBlock*>(ptr);
bool has = block >= mBlocks && block < mEndBlock;
return has;
}